

package dao

import (
	"errors"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/frame/gmvc"
	"guli-shopping/product-service/protobuff/productProto"
)

var (
	 //直接填充,this里也能用
	 PmsBrandDao = &pmsbrandDao{
		 M:g.DB().Table("pms_brand").Safe(),
	 }
)

type pmsbrandDao struct {
	gmvc.M
}

func (this *pmsbrandDao) FindAll(query *productProto.PmsBrandQuery) ([]*productProto.PmsBrand,int,error) {
    //分页,如果要添加条件,在这里添加
    count, _ := this.OmitEmpty().Count()
	all, err := this.OmitEmpty().Page(int(query.Page), int(query.Size)).All()
	models := []*productProto.PmsBrand{}
	err = all.Structs(&models)
	if err != nil {
		return nil,0,errors.New("查询失败")
	}
	return models,count,err


}

func (this *pmsbrandDao) FindById(id int64) (*productProto.PmsBrand,error) {
	all, err := this.WherePri(id).FindOne()
	if err != nil {
		return nil,errors.New("查询失败")
	}
	models := &productProto.PmsBrand{}
	all.Struct(models)
	return models,err
}

func (this *pmsbrandDao) Delete(ids[]int64) error {

	all, _ := this.WherePri(ids).Count()
	//要删除的数量不匹配
	if  len(ids)!=all{
		return errors.New("删除失败")
	}
	_, err := this.WherePri(ids).Delete()
	return err
}

func (this *pmsbrandDao) Update(req *productProto.PmsBrand) error {
	_, err := this.OmitEmpty().WherePri(req.BrandId).Data(req).Update()
	return err
}
func (this *pmsbrandDao) Save(req *productProto.PmsBrand) (int64,error) {
	all, err := this.Data(req).Save()
	if err!=nil {
		return 0,errors.New("保存失败")
	}
	id, _ := all.LastInsertId()
	return id,nil
}




